//
// Created by work on 24-11-4.
//

#ifndef CDATAUNIT_H
#define CDATAUNIT_H

#include <os/miscellany/stddef.h>

struct SDataUnit {
    enum RegisterType { Invalid, DiscreteInputs, Coils, InputRegisters, HoldingRegisters };
    enum SigType { Yx, Yc, Yk, Parameter };
    enum CtlType { Read, Write};

    RegisterType regtype;
    SigType sigtype;
    CtlType ctltype;

    uint16_t reg;

    int saddress;

    bool isbit;
    int bitoffest;

    bool isFilterCall;

    std::string user = "admin";
    std::string dev = "step_motor";
    std::string name;

    bool valid = false;

    bool fromJsonStream(const std::string& buf) {
        Json::Value root = stringToJson(buf);
        if(root.isNull()) return false;

        user = root["user"].asString();
        dev = root["dev"].asString();
        name = root["name"].asString();
        sigtype = static_cast<enum SigType>(root["sigtype"].asInt());
        ctltype = static_cast<enum CtlType>(root["ctltype"].asInt());
    }
};

#endif //CDATAUNIT_H
